package com.baidu.customview.widget;

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.Shader;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.OvalShape;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;

import com.baidu.customview.R;

/**
 * Created by imagine on 2015/11/9.
 */
public class ShapeView extends View {
    private ShapeDrawable drawable;

    public ShapeView(Context context) {
        super(context);
//        init();
    }


    public ShapeView(Context context, AttributeSet attrs) {
        super(context, attrs);
//        init();
    }

    public ShapeView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
//        init();
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public ShapeView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
//        init();
    }
    //圆形适应原图尺寸
    private void init() {
        drawable = new ShapeDrawable(new OvalShape());
//        drawable.getPaint().setColor(getResources().getColor(R.color.orange));
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.peien);
//        int width=bitmap.getWidth();
//        int height=bitmap.getHeight();
        Bitmap bit=Bitmap.createBitmap(bitmap,10,10,100,100);
        drawable.getPaint().setShader(new BitmapShader(bit, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP));
        Rect rect = new Rect(0, 0, 100,100);
        drawable.setBounds(rect);
    }
    private void picInit(){
        drawable=new ShapeDrawable(new OvalShape());
    }
    @Override
    protected void onDraw(Canvas canvas) {
//        drawable.draw(canvas);
        initDrawCircle(canvas);
    }
    public void initDrawCircle(Canvas canvas){
        drawable=new ShapeDrawable(new OvalShape());
        Bitmap bitmap=BitmapFactory.decodeResource(getResources(),R.mipmap.peien);
        int width=bitmap.getWidth();
        int height=bitmap.getHeight();
        int mSize=width>height?height:width;
        Bitmap bit=Bitmap.createBitmap(mSize,mSize,Bitmap.Config.ARGB_8888);
        drawable.getPaint().setShader(new BitmapShader(bit, Shader.TileMode.CLAMP,Shader.TileMode.CLAMP));
        Rect rect=new Rect(0,0,width,height);
        drawable.setBounds(rect);
        drawable.draw(canvas);
    }
}
